<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/6
 * Time: 10:15
 */
namespace app\web\controller;

use app\web\service\GoodsService;

class Goods extends Base {
    /**
     * @todo 在售商品列表
     * @author ZJF
     */
    public function getGoodsList(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $limit = request()->param('limit/d',10);//每页十条
        $limit = ($limit > 20)?20:$limit;//每页最多20条
        $pageNum = request()->param('pageNum/d',1);//页码
        $status = request()->param('status/d',1);//1上架状态
        $category_id = request()->param('category_id',0);//类目ID
        $goods_name = request()->param('goods_name','');//商品名称;
        $show_sku = request()->param('show_sku/d',0);//显示商品规格
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $goods_service = new GoodsService();
        $param = array(
            'shop_id'=>$shop_id,
            'shop_name'=>$user['login_id'],
            'limit'=>$limit,
            'page'=>$pageNum,
            'status'=>$status,
            'category_id'=>$category_id,
            'goods_name'=>$goods_name,
            'show_sku'=>1
        );
        $res = $goods_service->getGoodsList($shop_id,$param);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 获取自定义分类
     * @author ZJF
     */
    public function getCatlist(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $goods_service = new GoodsService();
        $param = array(
            'shop_id'=>$shop_id,
            'member_id'=>$user['member_id']
        );
        $res = $goods_service->getCatlist($shop_id,$param);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 编辑商品
     * @author ZJF
     */
    public function editGoods(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $data = request()->param('data','');//商品数据
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        if(empty($data)){
            return json_ok([],1,'无效的商品数据');
        }
        $data = json_decode($data,true);
        if(!is_array($data)){
            return json_ok([],1,'无效的商品数据');
        }
        $goods_service = new GoodsService();
        $param = array(
            'shop_id'=>$shop_id,
            'data'=>$data
        );
//        print_data($data);
        $res = $goods_service->editGoods($shop_id,$param);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res,0,"保存成功");
    }
}